<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\facade\View;
use app\common\model\LntegralCate;
use think\response\Json;

use app\common\facade\Builder;

class Lntegral extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Lntegral';

    //关联模型
    protected $hasOne = ['cate1'];

    public function index()
    {
        return $this->setTable();
    }
    public function setTable()
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80, 'fixed' => 'left'])
           
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'width' => 200, 'fixed' => 'left'])
            ->addColumn(['field' => 'icon', 'title' => '缩略图', 'width' => 90, 'templet' => '#iconTpl', 'fixed' => 'left'])
            ->addColumn([
                'title'   => '商品分类', 'width' => 200,
                'templet' => '<div>{{d.cate1.title}}</div>'
            ])
            ->addColumn(['field' => 'price', 'title' => '价格'])
            ->addColumn(['field' => 'stock', 'title' => '库存'])
            ->addColumn(['field' => 'create_time', 'title' => '添加时间', 'width' => 150])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间', 'width' => 150])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'width' => 120, 'templet' => '#sort_sort'])
            ->addColumn(['field' => 'status', 'title' => '上架状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addColumn(['field' => 'pick', 'title' => '是否开启推荐', 'templet' => '#pick', 'width' => 120, 'unresize' => true])
            //       

            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete'])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '上架|下架'],
                ['name' => 'pick', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->addSearch([
             ['title', '输入商品名称/描述', 'input', '', '', '', 'like'],
             ['cate_1', '商品专区', 'select', LntegralCate::where(['is_del' => 0])->column('title', 'id')]])
            ->addSortTool([['name' => 'sort', 'pk' => 'id', 'url' => url('update')->build()]])
            
            ->fetch();
    }


    public function add($tpl = 'lntegral/edit')
    {
        View::assign('cate', LntegralCate::getCate());
        View::assign('coupon', $this->model::getCoupon());
        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'lntegral/edit', $info = '')
    {
        $info = $this->model::find($id);
        View::assign('cate', LntegralCate::getCate());
        View::assign('coupon', $this->model::getCoupon());
        
        return parent::edit($id, $tpl, $info);
    }

    public function getProduct()
    {
        try {
            $param = $this->request->get();
            $list = $this->model::whereLike('title', '%' . $param['val'] . '%')
                ->field('id,title,icon,business_id,price,vip_price')->select();
            return json(['code' => 0, 'data' => $list]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }    public function saveProduct(): Json
    {
        return json($this->model::saveProduct($this->request->post())); // TODO: Change the autogenerated stub
    }

}
